package com.cargohu.aishouyu.system.controller;

import com.cargohu.aishouyu.auth.annotation.Permission;
import com.cargohu.aishouyu.common.CommonConstants;
import com.cargohu.aishouyu.framework.page.Paging;
import com.cargohu.aishouyu.framework.response.ApiResult;
import com.cargohu.aishouyu.system.dto.SysConfigAddDTO;
import com.cargohu.aishouyu.system.dto.SysConfigUpdateDTO;
import com.cargohu.aishouyu.system.query.SysConfigQuery;
import com.cargohu.aishouyu.system.service.SysConfigService;
import com.cargohu.aishouyu.system.vo.SysConfigVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 参数配置表 控制器
 *
 * @author yww
 * @since 2023-11-29
 */
@Slf4j
@RestController
@RequestMapping(CommonConstants.API_ADMIN_PATH+"/sysConfig")
@Tag(name = "参数配置")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 添加参数配置表
     *
     * @param sysConfigAddDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/addSysConfig")
    @Operation(summary = "添加参数配置")
    @Permission("sys:config:add")
    public ApiResult addSysConfig(@Valid @RequestBody SysConfigAddDTO sysConfigAddDTO) throws Exception {
        boolean flag = sysConfigService.addSysConfig(sysConfigAddDTO);
        return ApiResult.result(flag);
    }

    /**
     * 修改参数配置表
     *
     * @param sysConfigUpdateDTO
     * @return
     * @throws Exception
     */
    @PostMapping("/updateSysConfig")
    @Operation(summary = "修改参数配置")
    @Permission("sys:config:update")
    public ApiResult updateSysConfig(@Valid @RequestBody SysConfigUpdateDTO sysConfigUpdateDTO) throws Exception {
        boolean flag = sysConfigService.updateSysConfig(sysConfigUpdateDTO);
        return ApiResult.result(flag);
    }

    /**
     * 删除参数配置表
     *
     * @param configId
     * @return
     * @throws Exception
     */
    @PostMapping("/deleteSysConfig/{configId}")
    @Operation(summary = "删除参数配置")
    @Permission("sys:config:delete")
    public ApiResult deleteSysConfig(@PathVariable Long configId) throws Exception {
        boolean flag = sysConfigService.deleteSysConfig(configId);
        return ApiResult.result(flag);
    }

    /**
     * 获取参数配置表详情
     *
     * @param configId
     * @return
     * @throws Exception
     */
    @PostMapping("/getSysConfig/{configId}")
    @Operation(summary = "参数配置详情")
    @Permission("sys:config:info")
    public ApiResult<SysConfigVo> getSysConfig(@PathVariable Long configId) throws Exception {
        SysConfigVo sysConfigVo = sysConfigService.getSysConfigById(configId);
        return ApiResult.success(sysConfigVo);
    }

    /**
     * 参数配置表分页列表
     *
     * @param sysConfigQuery
     * @return
     * @throws Exception
     */
    @PostMapping("/getSysConfigList")
    @Operation(summary = "参数配置分页列表")
    @Permission("sys:config:list")
    public ApiResult<SysConfigVo> getSysConfigList(@Valid @RequestBody SysConfigQuery sysConfigQuery) throws Exception {
        Paging<SysConfigVo> paging = sysConfigService.getSysConfigList(sysConfigQuery);
        return ApiResult.success(paging);
    }

}
